# -*- coding: utf-8 -*-
import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDockWidget, QAction, QFileDialog
import cv2
import numpy as np
import copy
from imageManager import *

PARA_PATH = os.path.dirname(__file__)


class BlurGui(QtWidgets.QWidget):
    update_para_signal = QtCore.pyqtSignal(int, dict)
    para = dict()
    index = 0

    def __init__(self, parent=None):
        super(BlurGui, self).__init__(parent)
        self.imgMat = None

    def initPara(self):
        parafile = os.path.join(PARA_PATH, "module_para.json")
        with open(parafile, "r", encoding='utf-8') as f:
            self.para = json.loads(f.read())

    def setPara(self, index, para):
        self.index = index
        if para is None:
            return
        if para["version"] == self.para["version"]:
            self.para = para

        self.spinbox_kernel.setValue(self.para["value"])

    def initUI(self):
        self.initPara()
        label_kernel = QtWidgets.QLabel(self)
        label_kernel.setText("kernel size:")

        self.spinbox_kernel = QtWidgets.QSpinBox(self)
        self.spinbox_kernel.setMaximum(255)
        self.spinbox_kernel.setMinimum(1)
        self.spinbox_kernel.setSingleStep(2)
        self.spinbox_kernel.valueChanged.connect(self.kernel_changed)

        hbox1 = QtWidgets.QHBoxLayout()
        hbox1.addWidget(label_kernel)
        hbox1.addWidget(self.spinbox_kernel)

        btn_apply = QtWidgets.QPushButton(self)
        btn_apply.setText("apply")
        btn_apply.clicked.connect(self.btn_apply_clicked)

        btn_ok = QtWidgets.QPushButton(self)
        btn_ok.setText("ok")
        btn_ok.clicked.connect(self.btn_ok_clicked)

        btn_cancel = QtWidgets.QPushButton(self)
        btn_cancel.setText("cancel")
        btn_cancel.clicked.connect(self.btn_cancel_clicked)

        hbox3 = QtWidgets.QHBoxLayout()
        hbox3.addWidget(btn_ok)
        hbox3.addWidget(btn_cancel)

        vlayout = QtWidgets.QVBoxLayout(self)
        vlayout.addLayout(hbox1)
        vlayout.addWidget(btn_apply)
        vlayout.addStretch(1)
        vlayout.addLayout(hbox3)

    def kernel_changed(self):
        self.para["value"] = self.spinbox_kernel.value()

    def btn_apply_clicked(self):
        value = self.para["value"]
        img = img_mng.img
        if img is None:
            return
        ret = cv2.blur(img, (value, value))
        img_mng.img = ret
        img_mng.dispImg(ret)

    def btn_ok_clicked(self):
        self.para["value"] = self.spinbox_kernel.value()
        self.update_para_signal.emit(self.index, self.para)

    def btn_cancel_clicked(self):
        self.close()

    def run(self, para):
        value = para["value"]
        img = img_mng.img
        if img is None:
            return
        ret = cv2.blur(img, (value, value))
        img_mng.img = ret
        img_mng.dispImg(ret)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui = BlurGui()
    ui.initUI()
    ui.show()
    sys.exit(app.exec_())
